package com.yunbiao.ybbusstopplate.serialport

import android_serialport_api.SerialPort
import com.blankj.utilcode.util.LogUtils
import com.yunbiao.ybbusstopplate.utils.SpTool
import java.io.File
import java.io.InputStream
import java.io.OutputStream

class SerialPortTool {
    companion object{
        private var serialPortTool:SerialPortTool?=null
        fun getInstance():SerialPortTool?{
            if (serialPortTool==null){
                serialPortTool= SerialPortTool()
            }
            return serialPortTool
        }
        private var serialPort:SerialPort?=null
        private var isOpen:Boolean=false
        private var outputStream:OutputStream?=null
        private var inputStream:InputStream?=null
        private var readThread:ReadThread?=null
    }

    var iSerialPortListener: ISerialPortListener?=null
    fun setSerialPortListener(iSerialPortListener: ISerialPortListener?){
        this.iSerialPortListener=iSerialPortListener
    }

    fun open(){
        if (isOpen){
            return
        }
        try {
            if (iSerialPortListener!=null){
                iSerialPortListener?.start()
            }
            //获取设置的port
            val portType = SpTool.getConfigSerialPortType()
            if (portType==-1){
                if (iSerialPortListener!=null){
                    iSerialPortListener?.init(false)
                }
                return
            }
            //初始化portType
            SerialPortConfig.init(portType)
//            serialPort = SerialPort(File(SerialPortConfig.UART_PATH), SerialPortConfig.BAUDRATE, 0)
            serialPort = SerialPort(File(SerialPortConfig.UART_PATH), SerialPortConfig.BAUDRATE, 1,8,0,0,0)

            outputStream = serialPort!!.outputStream
            inputStream = serialPort!!.inputStream

            readThread= ReadThread(inputStream,iSerialPortListener)
            readThread!!.start()

            isOpen=true

            iSerialPortListener?.init(true)

        } catch (e: Exception) {
            e.printStackTrace()
            isOpen=false
            serialPort=null
            iSerialPortListener?.init(false)
            return
        }
    }

    fun close(){
        try {
            if (readThread!=null){
                readThread?.interrupt()
                readThread=null
            }
        } catch (e: Exception) {
        }

        try {
            if (outputStream!=null){
                outputStream?.close()
                outputStream=null
            }
        } catch (e: Exception) {
        }
        try {
            if (inputStream!=null){
                inputStream?.close()
                inputStream=null
            }
        } catch (e: Exception) {
        }
        try {
            if (serialPort!=null){
                serialPort?.close()
                serialPort=null
            }
        } catch (e: Exception) {
        }

        if (iSerialPortListener!=null){
            iSerialPortListener?.close()
        }
        isOpen=false
    }
}